Skip to content

fix(workspace): prevent stale placeholder data from corrupting workflow registry on switch#3642

Merged
waleedlatif1 merged 3 commits intostagingfrom
fix/workspace-switch-triple-reload
Mar 17, 2026
Merged

fix(workspace): prevent stale placeholder data from corrupting workflow registry on switch#3642
waleedlatif1 merged 3 commits intostagingfrom
fix/workspace-switch-triple-reload

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • keepPreviousData in useWorkflows was causing the previous workspace's workflows to briefly flash when switching workspaces — TanStack Query v5 sets status = 'success' and isPlaceholderData = true when placeholder data is available, so the completeMetadataLoad effect was firing with stale data from the wrong workspace
  • Guard completeMetadataLoad with !query.isPlaceholderData so it only populates the registry with real fetched data
  • Trigger beginMetadataLoad when isPlaceholderData is true to correctly show loading state on direct URL navigation (where switchToWorkspace is not called)
  • Narrow useCreateWorkspace invalidation from workspaceKeys.all to workspaceKeys.lists() — the old code was unnecessarily invalidating permissions, settings, and members for all workspaces

Type of Change

  • Bug fix

Testing

Tested manually — workspace switching no longer flashes the previous workspace's content before showing the new one

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

waleedlatif1 and others added 2 commits March 17, 2026 15:58
Calls abortAllInProgressTools in the stream_end handler so tools
stuck in generating/executing state are resolved when the stream
closes, regardless of whether a done event was received.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ow registry on switch

- Guard completeMetadataLoad to skip when isPlaceholderData is true, preventing
  the previous workspace's workflows from briefly populating the registry during switch
- Trigger beginMetadataLoad when isPlaceholderData is true to ensure loading state
  is shown correctly on direct URL navigation (no switchToWorkspace call)
- Narrow useCreateWorkspace invalidation from workspaceKeys.all to workspaceKeys.lists()
  to avoid unnecessary refetches of permissions, settings, and members
@cursor
Copy link

cursor bot commented Mar 17, 2026

You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace.

To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard.

@vercel
Copy link

vercel bot commented Mar 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Mar 17, 2026 11:14pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 17, 2026

Greptile Summary

This PR fixes a race condition where stale placeholder data from the previous workspace briefly populated the workflow registry during workspace switching, and also cleans up two related issues: missing beginMetadataLoad for direct URL navigation and overly broad cache invalidation in useCreateWorkspace.

Key changes:

  • hooks/queries/workflows.ts: Guards completeMetadataLoad with !query.isPlaceholderData so the workflow registry is only populated with freshly-fetched data. Adds isPlaceholderData as a trigger for beginMetadataLoad to correctly display the loading state when TanStack Query serves placeholder data (the keepPreviousData path on workspace switch).
  • hooks/queries/workspace.ts: Replaces the broad workspaceKeys.all invalidation in useCreateWorkspace.onSuccess with targeted workspaceKeys.lists() + workspaceKeys.adminLists() invalidation. The previous removeQueries calls on newly created workspace's detail/settings/permissions/members keys are also removed (they were no-ops since those keys would never have been cached for a brand-new workspace).
  • lib/copilot/client-sse/handlers.ts: Calls abortAllInProgressTools at stream_end to ensure any tool calls still marked as in-progress are resolved when the SSE stream terminates, preventing tool calls from remaining stuck in an executing state.

Confidence Score: 4/5

  • This PR is safe to merge — the changes are targeted, logically sound, and address a real UX bug with no apparent regressions.
  • The workflows.ts and workspace.ts changes are straightforward and correct: the isPlaceholderData guard precisely solves the stale-data corruption, and the narrowed invalidation avoids unnecessary cache busting. The handlers.ts change is also sensible — abortAllInProgressTools already exists and is used from cleanupActiveState, so calling it from stream_end is a consistent cleanup path. Score is 4 rather than 5 primarily because no tests were added or updated to cover these code paths, leaving the correctness of the copilot stream-end cleanup unverified by automated tests.
  • No files require special attention; all changes are well-scoped and do not touch security-sensitive paths.

Important Files Changed

Filename Overview
apps/sim/hooks/queries/workflows.ts Guards completeMetadataLoad with !query.isPlaceholderData and triggers beginMetadataLoad on placeholder data to fix stale registry population during workspace switching.
apps/sim/hooks/queries/workspace.ts Narrows useCreateWorkspace invalidation from workspaceKeys.all to workspaceKeys.lists() and workspaceKeys.adminLists(), avoiding unnecessary cache busting of per-workspace detail, settings, members, and permissions queries.
apps/sim/lib/copilot/client-sse/handlers.ts Adds abortAllInProgressTools call in stream_end handler to clean up any still-pending tool calls when the stream terminates; the get parameter is now consumed for this purpose.

Sequence Diagram

sequenceDiagram
    participant User
    participant useWorkflows
    participant TanStackQuery as TanStack Query
    participant Registry as Workflow Registry

    User->>useWorkflows: Switch workspace (workspaceId changes)
    Note over TanStackQuery: keepPreviousData kicks in
    TanStackQuery-->>useWorkflows: status='success', isPlaceholderData=true (stale data from old workspace)

    Note over useWorkflows: BEFORE fix: completeMetadataLoad fires<br/>with stale data → corrupt registry

    Note over useWorkflows: AFTER fix:
    useWorkflows->>Registry: beginMetadataLoad(newWorkspaceId) [isPlaceholderData=true]
    Note over Registry: Loading state shown

    TanStackQuery->>TanStackQuery: Fetch new workspace workflows
    TanStackQuery-->>useWorkflows: status='success', isPlaceholderData=false (real data)

    useWorkflows->>Registry: completeMetadataLoad(newWorkspaceId, realData)
    Note over Registry: Registry populated with correct workflows
Loading

Last reviewed commit: "fix(workspace): also..."

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 75a3e2c into staging Mar 17, 2026
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/workspace-switch-triple-reload branch March 17, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant